<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link crossorigin="anonymous"
    integrity="sha512-drnvWxqfgcU6sLzAJttJv7LKdjWn0nxWCSbEAtxJ/YYaZMyoNLovG7lPqZRdhgL1gAUfa+V7tbin8y+2llC1cw=="
    href="https://lib.baomitu.com/twitter-bootstrap/4.5.3/css/bootstrap.css" rel="stylesheet">
  <script crossorigin="anonymous"
    integrity="sha512-YXLGLsQBiwHPHLCAA9npZWhADUsHECjkZ71D1uzT2Hpop82/eLnmFb6b0jo8pK4T0Au0g2FETrRJNblF/46ZzQ=="
    src="https://lib.baomitu.com/vue/2.6.12/vue.js"></script>
</head>

<body>
  <div id="app">

    <make-table :list-title="listTitle" :list-data="listData"></make-table>

  </div>

  <template id="mpage">
    <nav>
      <ul class="pagination">
        <li v-show="page !== 1" class="page-item">
          <a @click="topage(page - 1)" class="page-link">Previous</a>
        </li>
        <li v-for="number in pageSum" :class="number == page ? 'page-item active' : 'page-item'"><a class="page-link"
            href="#" @click="topage(number)">{{number}}</a></li>
        <li v-show="page !== pageSum" class="page-item">
          <a @click="topage(page + 1)" class="page-link" href="#">Next</a>
        </li>
      </ul>
    </nav>
  </template>

  <template id="mtable">
    <div>
      <table class="table">
        <thead>
          <tr>
            <th v-for="item in listTitle" :key="item">{{item}}</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="item in listPage" :key="item.order">
            <td>{{item.order}}</td>
            <td>{{item.name}}</td>
            <td>{{item.age}}</td>
            <td>{{item.major}}</td>
          </tr>
        </tbody>
      </table>
      <make-page @mpage-topage="updatePage" :page-size="pageSize" :data-sum="listData.length" :page="page">
        </ma-page>
    </div>
  </template>
  <script>
    const Mpage = Vue.extend({
      props: ['pageSize', 'dataSum', 'page'],
      template: '#mpage',
      computed: {
        pageSum: function () {
          return Math.ceil(this.dataSum / this.pageSize);
        },
      },
      methods: {
        topage(page) {
          this.$emit('mpage-topage', page)
        }
      }
    })

    const Mtable = Vue.extend({
      props: ['listTitle', 'listData'],
      template: '#mtable',
      components: {
        'make-page': Mpage
      },
      data: function () {
        return {
          page: 2,
          pageSize: 3,
        }
      },
      computed: {
        listPage: function () {
          let offset = (this.page - 1) * this.pageSize;
          return this.listData.slice(offset, offset + this.pageSize);
        }
      },
      methods: {
        updatePage(p) {
          this.page = p;
        }
      },
    })



    const myApp = new Vue({
      el: '#app',
      data: {
        listTitle: [
          'Order Number',
          'Name',
          'Age',
          'Major'
        ],
        listData: [
          {
            name: '小明',
            age: 20,
            major: '计算机',
          },
          {
            name: '小王',
            age: 20,
            major: '计算机',
          },
          {
            name: '小李',
            age: 20,
            major: '计算机',
          },
          {
            name: '小白',
            age: 20,
            major: '计算机',
          },
          {
            name: '小杨',
            age: 20,
            major: '计算机',
          },
          {
            name: '小A',
            age: 20,
            major: '计算机',
          },
          {
            name: '小C',
            age: 20,
            major: '计算机',
          },
          {
            name: '小朱',
            age: 20,
            major: '计算机',
          },
          {
            name: '小fj',
            age: 20,
            major: '计算机',
          },
        ],
      },
      components: {
        'make-table': Mtable,
      },
      created() {
        this.listData = this.listData.map((v, i) => {
          v.order = i;
          return v;
        })
      }
    });

  </script>
</body>

</html>